<?php
abstract class abstract_user_dao extends Model {

    protected $table_user;
    protected $table_group;
    protected $user_type;

    public function get_user_type(){
        return $this->user_type;
    }

    public function login($username, $password) {
        if($user = $this->get_by_username($username)){
            if($user->login($username, $password)) {
                return $user;
            }
        }
        return false;
    }

    public function logout(interface_user $user){
    }

    public function update_session(interface_user $user, $session){
    }

    public function update_user(interface_user $user, $session){
    }

    public function get_by_email($email) {

        $user_query = $this->pdo->prepare("SELECT * 
            FROM " . $this->table_user . " 
            WHERE email = :email 
            AND status = 1");

        $user_query->setFetchMode(
            PDO::FETCH_CLASS, 
            $this->user_type
        );

        $user_query->execute(array(':email' => $email));

        if($user_query->rowCount() != 1){
            return false;
        }

        return $user_query->fetch();

    }

    public function get_by_username($username) {

        $user_query = $this->pdo->prepare("SELECT * 
            FROM " . $this->table_user . " 
            WHERE username = :username 
            AND status = 1");

        $user_query->setFetchMode(
            PDO::FETCH_CLASS, 
            $this->user_type
        );

        $user_query->execute(array(':username' => $username));

        if($user_query->rowCount() != 1){
            return false;
        }

        return $user_query->fetch();

    }

    public function get_by_token($token) {

        $delete = $this->pdo->prepare("UPDATE 
            " . $this->table_user . " 
            SET token = ''
            WHERE token = :token");

        $query = $this->pdo->prepare("SELECT * 
            FROM " . $this->table_user . " 
            WHERE token = :token 
            AND token != ''");
        
        $query->setFetchMode(PDO::FETCH_CLASS, $this->user_type);

        $query->execute(array(':token' => $token));

        $user = $query->fetch();

        $delete->execute(array(':token' => $token));

        return $user;
    }

    public function get_by_id($user_id) {

        $user_query = $this->pdo->prepare("SELECT * FROM 
            " . $this->table_user . " 
            WHERE user_id = :user_id 
            AND status = '1'");

        $user_query->setFetchMode(
            PDO::FETCH_CLASS, 
            $this->user_type
        );

        $user_query->execute(array(
            ':user_id' => (int)$user_id,
        ));

        return $user_query->fetch();
    }

    public function get_permissions(interface_user $user) {

        $user_group_query = $this->pdo->prepare("SELECT permission 
            FROM " . $this->table_group . " 
            WHERE user_group_id = :user_group_id");

        $user_group_query->execute(array(
            ':user_group_id' => $user->get_group_id()
        ));

        $permission = array();
        $permissions  = $user_group_query->fetch(PDO::FETCH_ASSOC);
        $permissions  = unserialize($permissions['permission']);

        if (is_array($permissions)) {
            foreach ($permissions as $key => $value) { 
                $permission[$key] = $value;
            }
        }
        return $permission;
    }

    public function update_ip(interface_user $user, $ip){
        $update_ip = $this->pdo->prepare("UPDATE " . $this->table_user . " 
            SET ip = :ip  
            WHERE user_id = :user_id");

        $update_ip->execute(array(
            ':ip' => $ip,
            ':user_id'  => $user->get_id()
        ));
    }
}
